define(["jquery", "easy-admin", "echarts", "echarts-theme", "miniAdmin", "miniTheme", "miniTab", "swiper"], function ($, ea, echarts, undefined, miniAdmin, miniTheme, miniTab) {

    return {
        index: function () {
            var options = {
                iniUrl: ea.url('ajax/initAdmin'),    // 初始化接口
                clearUrl: ea.url("ajax/clearCache"), // 缓存清理接口
                urlHashLocation: true,      // 是否打开hash定位
                bgColorDefault: false,      // 主题默认配置
                multiModule: true,          // 是否开启多模块
                menuChildOpen: false,       // 是否默认展开菜单
                loadingTime: 0,             // 初始化加载时间
                pageAnim: true,             // iframe窗口动画
                maxTabNum: 20,              // 最大的tab打开数量
            };
            miniAdmin.render(options);

            $('.login-out').on("click", function () {
                ea.request.get({
                    url: 'login/out',
                    prefix: true,
                }, function (res) {
                    ea.msg.success(res.msg, function () {
                        window.location = ea.url('login/index');
                    })
                });
            });
        },
        welcome: function () {
            miniTab.listen();

            new Swiper('.mySwiper', {
                pagination: {
                    el: '.swiper-pagination',
                    clickable: true,
                },
            })

            /**
             * 查看公告信息
             **/
            $('body').on('click', '.layuimini-notice', function () {
                var title = $(this).children('.layuimini-notice-title').text(),
                    noticeTime = $(this).children('.layuimini-notice-extra').text(),
                    content = $(this).children('.layuimini-notice-content').html();
                var html = '<div style="padding:15px 20px; text-align:justify; line-height: 22px;border-bottom:1px solid #e2e2e2;background-color: #2f4056;color: #ffffff">\n' +
                    '<div style="text-align: center;margin-bottom: 20px;font-weight: bold;border-bottom:1px solid #718fb5;padding-bottom: 5px"><h4 class="text-danger">' + title + '</h4></div>\n' +
                    '<div style="font-size: 12px">' + content + '</div>\n' +
                    '</div>\n';
                layer.open({
                    type: 1,
                    title: '系统公告' + '<span style="float: right;right: 1px;font-size: 12px;color: #b1b3b9;margin-top: 1px">' + noticeTime + '</span>',
                    area: '300px;',
                    shade: 0.8,
                    id: 'layuimini-notice',
                    btn: ['查看', '取消'],
                    btnAlign: 'c',
                    moveType: 1,
                    content: html,
                    success: function (layero) {
                        var btn = layero.find('.layui-layer-btn');
                        btn.find('.layui-layer-btn0').attr({
                            href: 'https://gitee.com/zhongshaofa/layuimini',
                            target: '_blank'
                        });
                    }
                });
            });

            // 初始化报表
            baobiao();

            // 数据统计异步查询
            $(".layui-badge").click(function() {
                // 修改按钮样式
                var title=$(this).attr('title');
                $("."+title).removeClass('tt');
                $(this).addClass("tt");
                // 改变数据
                var text=$(this).text();
                var time_type_obj={'年':1,'月':2,'日':3};//查询时间
                var time_type=time_type_obj[text];
    
                var card_type_obj={'goods-time':1,'cash-time':2,'deal-time':3};//查询类型
                var card_type=card_type_obj[title];
    
                addLinkUrl = "ajaxData"
    
                $.ajax({
                    url:addLinkUrl,
                    data: {'time_type':time_type,'card_type':card_type},
                    type:'POST',
                    success:function(result){
                        if (result.code == 1) {
                           if(card_type==1){
                             $('.goods-time-h1').text(result.data.goods_order)
                             $('.goods-time-h5').text(result.data.goods_order_no_send)
                           }
                           if(card_type==2){
                             $('.cash-time-h1').text(result.data.cash_order)
                             $('.cash-time-h5').text(result.data.cash_order_no_send)
                           }
                           if(card_type==3){
                             $('.deal-time-h1').text(result.data.deal_num)
                             $('.deal-time-h5').text(result.data.deal_money)
                           }
                        }else{
                            layer.alert(result.msg, {icon: 2});
                        }
                    },
                    error:function (res) {
                        layer.alert(result.msg, {icon: 2});
                    }
                });
    
            }) 

            // 统计报表异步查询
            $(".sel_type").change(function() {
                addLinkUrl = "ajaxLine"
                $.ajax({
                    url:addLinkUrl,
                    data: {'sel_type':$(".sel_type").val()},
                    type:'POST',
                    success:function(result){
                        if (result.code == 1) {
                            
                          var goods_num=result.data.goods_num
                        //   alert(JSON.stringify(key_name))
                          $('#goods_num').val(JSON.stringify(goods_num));
    
                          var cash_num=result.data.cash_num
                          $('#cash_num').val(JSON.stringify(cash_num));
    
                          var totel_num=result.data.totel_num
                          $('#totel_num').val(JSON.stringify(totel_num));
    
                          var key_name=result.data.key_name
                          $('#key_name').val(JSON.stringify(key_name));

                          baobiao();                          
                        }else{
                            layer.alert(result.msg, {icon: 2});
                        }
                    },
                    error:function (res) {
                        layer.alert(result.msg, {icon: 2});
                    }
                });
            })


            /**
             * 报表功能
             */
            function baobiao() {
                $('#layui-version').text('v' + layui.v);
                let echartsRecords = echarts.init(document.getElementById('echarts-records'), 'walden');
                let optionRecords = {
                    title: {
                        text: '访问统计'
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['商品订单扣费金额', '虚拟订单扣费金额', '全部订单扣费金额']
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    toolbox: {
                        feature: {
                            saveAsImage: {}
                        }
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data:JSON.parse($('#key_name').val())
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [
                        {
                            name: '商品订单扣费金额',
                            type: 'line',
                            stack: '总量',
                            data: JSON.parse($('#goods_num').val())
                        },
                        {
                            name: '虚拟订单扣费金额',
                            type: 'line',
                            stack: '总量',
                            data: JSON.parse($('#cash_num').val())
                        },
                        {
                            name: '全部订单扣费金额',
                            type: 'line',
                            stack: '总量',
                            data: JSON.parse($('#totel_num').val())
                        },
                    ]
                };
                echartsRecords.setOption(optionRecords);
                window.addEventListener("resize", function () {
                    echartsRecords.resize();
                });
            }
        },
        editAdmin: function () {
            let form = layui.form
            form.on('radio(loginType-filter)', function (data) {
                let elem = data.elem
                let value = elem.value
                if (value === '2') {
                    let width = screen.width < 768 ? '85%' : '60%'
                    ea.open('绑定谷歌验证码', ea.url('index/set2fa'), width, '75%')
                }
            });
            ea.listen();
        },
        editPassword: function () {
            ea.listen();
        },
        set2fa: function () {
            ea.listen();
        },
    };
});
